package com.zelin.manager.web;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.dubbo.config.annotation.Reference;
import com.zelin.entity.SpecificationEntity;
import com.zelin.entity.group.Specification;
import com.zelin.entity.vo.SpecificationVoEntity;
import com.zelin.sellergoods.service.SpecificationService;
import com.zelin.utils.PageUtils;
import com.zelin.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 
 *
 * @author WF
 * @email 2568783935@qq.com
 * @date 2021-07-23 21:10:35
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController {
    @Reference(timeout = 5000)
    private SpecificationService specificationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("manager:specification:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = specificationService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 功能: 显示所有规格列表
     * 参数:
     * 返回值: com.zelin.utils.R
     * 时间: 2021/7/27 11:57
     */
    @RequestMapping("/findAll")
    public R findAll(){
        //1. 查询所有规格列表
        List<SpecificationEntity> list = specificationService.list();
        //2. 收集数据转换为SpecificationVoEntity对象
        List<SpecificationVoEntity> collect = list.stream()
                                                  .map(m -> new SpecificationVoEntity(m.getId(), m.getSpecName()))
                                                  .collect(Collectors.toList());
        //3. 返回分页对象
        return R.ok().put("list", collect);
    }

    /**
     * 信息(根据id查询规格)
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("manager:specification:info")
    public R info(@PathVariable("id") Long id){
		Specification specification = specificationService.findById(id);

        return R.ok().put("specification", specification);
    }

    /**
     * 保存(添加)
     */
    @RequestMapping("/save")
    //@RequiresPermissions("manager:specification:save")
    public R save(@RequestBody Specification specification){
        System.out.println("specification--->add = " + specification);
		specificationService.add(specification);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("manager:specification:update")
    public R update(@RequestBody Specification specification){
		specificationService.update2(specification);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("manager:specification:delete")
    public R delete(@RequestBody Long[] ids){
		specificationService.batchDelete(Arrays.asList(ids));

        return R.ok();
    }

}
